************************************************************************************************************************************************************
* this do-file is part of the replication package for "Committee Decision-Making under the Threat of Leaks" by Fehrler and Hahn (JOP)
* it reads in zTree raw data from all experimental sessions and recodes it for analysis.do
	* two data sets are created:
		* alldata.dta is the complete data set without any new variables
		* alldata-rec.dta is the complete data set including newly created variables for the analysis
	* path needs to be set in line 10
************************************************************************************************************************************************************

//global path "[Insert your replication-files folder path here]"
global path "C:\Users\tempora\Dropbox\CommitteeLeaks\Experiment\stata\replication-files"

cd $path
cd raw-data

clear

insheet using 171127_0903_1_subjects_sbj.xls, tab case name
gen tleaks=1
gen tsecrecy=0
gen ttransparency=0
gen sessionid=1

cd $path
save session_leaks_1.dta, replace
cd raw-data

clear
insheet using 171129_0906_1_subjects_sbj.xls, tab case name
gen tleaks=1
gen tsecrecy=0
gen ttransparency=0
gen sessionid=2

cd $path
save session_leaks_2.dta, replace
cd raw-data

clear
insheet using 171212_0908_1_subjects_sbj.xls, tab case name
gen tleaks=1
gen tsecrecy=0
gen ttransparency=0
gen sessionid=3

cd $path
save session_leaks_3.dta, replace
cd raw-data

clear
insheet using 171113_0929_1_subjects_sbj.xls, tab case name
gen tleaks=0
gen tsecrecy=1
gen ttransparency=0
gen sessionid=4

cd $path
save session_secrecy_1.dta, replace
cd raw-data

clear
insheet using 171120_0927_1_subjects_sbj.xls, tab case name
gen tleaks=0
gen tsecrecy=1
gen ttransparency=0
gen sessionid=5

cd $path
save session_secrecy_2.dta, replace
cd raw-data

clear
insheet using 180207_1136_1_subjects_sbj.xls, tab case name
gen tleaks=0
gen tsecrecy=1
gen ttransparency=0
gen sessionid=6

cd $path
save session_secrecy_3.dta, replace
cd raw-data

clear
insheet using 171120_1127_1_subjects_sbj.xls, tab case name
gen tleaks=0
gen tsecrecy=0
gen ttransparency=1
gen sessionid=7

encode Abinote, gen(abinote)
drop Abinote
ren abinote Abinote
encode Mathenote, gen(mathenote)
drop Mathenote
ren mathenote Mathenote

cd $path
save session_transparency_1.dta, replace
cd raw-data

clear
insheet using 171212_1124_1_subjects_sbj.xls, tab case name
gen tleaks=0
gen tsecrecy=0
gen ttransparency=1
gen sessionid=8

cd $path
save session_transparency_2.dta, replace
cd raw-data

clear
insheet using 180207_0927_1_subjects_sbj.xls, tab case name
gen tleaks=0
gen tsecrecy=0
gen ttransparency=1
gen sessionid=9

cd $path
save session_transparency_3.dta, replace

//append

use session_leaks_1.dta
append using session_leaks_2.dta
append using session_leaks_3.dta
append using session_secrecy_1.dta
append using session_secrecy_2.dta
append using session_secrecy_3.dta
append using session_transparency_1.dta
append using session_transparency_2.dta
append using session_transparency_3.dta

gen treatment=.
replace treatment=1 if tsecrecy==1
replace treatment=2 if ttransparency==1
replace treatment=3 if tleaks==1

lab def treatment 1 "secrecy" 2 "transparency" 3 "leaks"
lab val treatment treatment

tab ColorType, gen(ct_)
ren ct_1 light_type
ren ct_2 dark_type
ren ct_3 neutral_type

tab State, gen(state_)
ren state_1 light_state
ren state_2 dark_state
ren state_3 red_state

tab BallColor, gen(ballc_)
replace ballc_1=. if Advisor==0
replace ballc_2=. if Advisor==0
replace ballc_3=. if Advisor==0
replace ballc_4=. if Advisor==0

ren ballc_1 gray_ball
ren ballc_2 light_ball
ren ballc_3 dark_ball
ren ballc_4 red_ball

replace Message=. if Advisor==0
replace Message=. if  gray_ball==1

gen scenario=.
replace scenario=1 if MatchingGroup==1 & Part==1
replace scenario=1 if MatchingGroup==2 & Part==2
replace scenario=2 if MatchingGroup==1 & Part==2
replace scenario=2 if MatchingGroup==2 & Part==1

replace RecomVote=. if Advisor==0
replace VoteCommittee=. if Advisor==1
replace Leak=. if Advisor==0
replace Leak=. if treatment!=3
replace AnyLeak=. if treatment!=3


drop Plight Pdark Pinfo red light dark middle line*

drop if Period < 4 

ren Mnnlich male

save alldata.dta, replace


//new variables for analysis

egen session_id = group(treatment SessionID)
egen sid = group(session_id Subject)
egen mid = group(session_id MatchingGroup)
egen gid = group(mid Group)
egen gpid = group(gid Period)

order SessionID session_id sid mid gid gpid

bysort gpid: egen sdpoints=sd(Points)

tab treat,gen(tr_)

gen red_s_sec=red_state*tsecrecy
gen red_s_leaks=red_state*tleaks
gen red_s_transp=red_state*ttransparency

gen blue_s_sec=(red_state==0)*tsecrecy
gen blue_s_leaks=(red_state==0)*tleaks
gen blue_s_transp=(red_state==0)*ttransparency
gen blue_state=red_state==0

gen secr_sc1=(treatment==1 & scenario==1)
gen secr_sc2=(treatment==1 & scenario==2)
gen tran_sc1=(treatment==2 & scenario==1)
gen tran_sc2=(treatment==2 & scenario==2)
gen leak_sc1=(treatment==3 & scenario==1)
gen leak_sc2=(treatment==3 & scenario==2)

gen state_known=(CommState==1 & ttransp) | (AnyLeak==1)
gen bad_state_known = state_known & (State!=ColorType)
gen good_state_known = state_known & (State==ColorType)

gen recom_1=0
gen recom_2=0
gen recom_3=0

order SessionID session_id sid mid Group ColorType Advisor RecomVote recom_1 recom_2 recom_3
sort mid Period Group ColorType Advisor

replace recom_1=1 if Advisor==1 & ColorType==1 & RecomVote==1
replace recom_1=1 if Advisor[_n+1]==1 & ColorType[_n+1]==1 & RecomVote[_n+1]==1 & Group==Group[_n+1]
replace recom_1=1 if Advisor[_n+2]==1 & ColorType[_n+2]==1 & RecomVote[_n+2]==1 & Group==Group[_n+2]
replace recom_1=1 if Advisor[_n+3]==1 & ColorType[_n+3]==1 & RecomVote[_n+3]==1 & Group==Group[_n+3]
replace recom_1=1 if Advisor[_n+4]==1 & ColorType[_n+4]==1 & RecomVote[_n+4]==1 & Group==Group[_n+4]
replace recom_1=1 if Advisor[_n-1]==1 & ColorType[_n-1]==1 & RecomVote[_n-1]==1 & Group==Group[_n-1]
replace recom_1=1 if Advisor[_n-2]==1 & ColorType[_n-2]==1 & RecomVote[_n-2]==1 & Group==Group[_n-2]
replace recom_1=1 if Advisor[_n-3]==1 & ColorType[_n-3]==1 & RecomVote[_n-3]==1 & Group==Group[_n-3]
replace recom_1=1 if Advisor[_n-4]==1 & ColorType[_n-4]==1 & RecomVote[_n-4]==1 & Group==Group[_n-4]

replace recom_2=1 if Advisor==1 & ColorType==2 & RecomVote==1
replace recom_2=1 if Advisor[_n+1]==1 & ColorType[_n+1]==2 & RecomVote[_n+1]==1 & Group==Group[_n+1]
replace recom_2=1 if Advisor[_n+2]==1 & ColorType[_n+2]==2 & RecomVote[_n+2]==1 & Group==Group[_n+2]
replace recom_2=1 if Advisor[_n+3]==1 & ColorType[_n+3]==2 & RecomVote[_n+3]==1 & Group==Group[_n+3]
replace recom_2=1 if Advisor[_n+4]==1 & ColorType[_n+4]==2 & RecomVote[_n+4]==1 & Group==Group[_n+4]
replace recom_2=1 if Advisor[_n-1]==1 & ColorType[_n-1]==2 & RecomVote[_n-1]==1 & Group==Group[_n-1]
replace recom_2=1 if Advisor[_n-2]==1 & ColorType[_n-2]==2 & RecomVote[_n-2]==1 & Group==Group[_n-2]
replace recom_2=1 if Advisor[_n-3]==1 & ColorType[_n-3]==2 & RecomVote[_n-3]==1 & Group==Group[_n-3]
replace recom_2=1 if Advisor[_n-4]==1 & ColorType[_n-4]==2 & RecomVote[_n-4]==1 & Group==Group[_n-4]

replace recom_3=1 if Advisor==1 & ColorType==3 & RecomVote==1
replace recom_3=1 if Advisor[_n+1]==1 & ColorType[_n+1]==3 & RecomVote[_n+1]==1 & Group==Group[_n+1]
replace recom_3=1 if Advisor[_n+2]==1 & ColorType[_n+2]==3 & RecomVote[_n+2]==1 & Group==Group[_n+2]
replace recom_3=1 if Advisor[_n+3]==1 & ColorType[_n+3]==3 & RecomVote[_n+3]==1 & Group==Group[_n+3]
replace recom_3=1 if Advisor[_n+4]==1 & ColorType[_n+4]==3 & RecomVote[_n+4]==1 & Group==Group[_n+4]
replace recom_3=1 if Advisor[_n-1]==1 & ColorType[_n-1]==3 & RecomVote[_n-1]==1 & Group==Group[_n-1]
replace recom_3=1 if Advisor[_n-2]==1 & ColorType[_n-2]==3 & RecomVote[_n-2]==1 & Group==Group[_n-2]
replace recom_3=1 if Advisor[_n-3]==1 & ColorType[_n-3]==3 & RecomVote[_n-3]==1 & Group==Group[_n-3]
replace recom_3=1 if Advisor[_n-4]==1 & ColorType[_n-4]==3 & RecomVote[_n-4]==1 & Group==Group[_n-4]

gen recom_same=.
replace recom_same=0 if Advisor==0
replace recom_same=1 if Advisor==0 & ((ColorType==1 & recom_1==1) | (ColorType==2 & recom_2==1))

gen recom_other=.
replace recom_other=0 if Advisor==0
replace recom_other=1 if Advisor==0 & ((ColorType==1 & recom_2==1) | (ColorType==2 & recom_1==1))

gen scenround=Period-3 if Period<14
replace scenround=Period-13 if Period>13

//Best-Response Communication

gen eq_communication=.
replace eq_communication=1 if (treat==1) & (Message==1) & (BallColor==ColorType)
replace eq_communication=1 if (treat==2) & (Message==0) & (BallColor==ColorType)
replace eq_communication=1 if (treat==3) & (Message==0) & (BallColor==ColorType)
replace eq_communication=0 if (treat==1) & (Message==0) & (BallColor==ColorType)
replace eq_communication=0 if (treat==2) & (Message==1) & (BallColor==ColorType)
replace eq_communication=0 if (treat==3) & (Message==1) & (BallColor==ColorType)

replace eq_communication=1 if (treat==1) & (Message==0) & (BallColor!=ColorType) & (ColorType!=3) & (BallColor!=0) &  (BallColor!=5)
replace eq_communication=1 if (treat==2) & (Message==1) & (BallColor!=ColorType) & (ColorType!=3) & (BallColor!=0) &  (BallColor!=5)
replace eq_communication=1 if (treat==3) & (Message==1) & (BallColor!=ColorType) & (ColorType!=3) & (BallColor!=0) &  (BallColor!=5)
replace eq_communication=0 if (treat==1) & (Message==1) & (BallColor!=ColorType) & (ColorType!=3) & (BallColor!=0) &  (BallColor!=5)
replace eq_communication=0 if (treat==2) & (Message==0) & (BallColor!=ColorType) & (ColorType!=3) & (BallColor!=0) &  (BallColor!=5)
replace eq_communication=0 if (treat==3) & (Message==0) & (BallColor!=ColorType) & (ColorType!=3) & (BallColor!=0) &  (BallColor!=5)

replace eq_communication=1 if (treat==1) & (Message==1) &  (BallColor!=5) & (ColorType==3) & (BallColor!=0)
replace eq_communication=1 if (treat==2) & (Message==0) &  (BallColor!=5) & (ColorType==3) & (BallColor!=0)
replace eq_communication=1 if (treat==3) & (Message==0) &  (BallColor!=5) & (ColorType==3) & (BallColor!=0)
replace eq_communication=0 if (treat==1) & (Message==0) &  (BallColor!=5) & (ColorType==3) & (BallColor!=0)
replace eq_communication=0 if (treat==2) & (Message==1) &  (BallColor!=5) & (ColorType==3) & (BallColor!=0)
replace eq_communication=0 if (treat==3) & (Message==1) &  (BallColor!=5) & (ColorType==3) & (BallColor!=0)

replace eq_communication=1 if (Message==1) &  (BallColor==5)
replace eq_communication=0 if (Message==0) &  (BallColor==5)


ren Alter age
ren tr_1 tr_secrecy
ren tr_2 tr_transparency
ren tr_3 tr_leaks
ren  Advisor advisor
ren ColorType color_type
ren BallColor ball_color
ren Message message
ren State state
ren CommState comm_state
ren RecomVote recom_vote
ren AnyLeak any_leak
ren RecomCommittee recom_committee
ren VoteCommittee  vote_committee
ren DeciCommittee deci_committee
ren recom_3 recom_neutral
ren Leak leak



keep  sid mid gid gpid treatment tr_secrecy tr_transparency tr_leaks scenario scenround state red_state blue_state light_state dark_state red_s_sec red_s_leaks red_s_transp blue_s_sec blue_s_leaks blue_s_transp secr_sc1 secr_sc2 tran_sc1 tran_sc2 leak_sc1 leak_sc2 advisor color_type neutral_type dark_type light_type ball_color gray_ball light_ball dark_ball red_ball message comm_state eq_communication  state_known bad_state_known good_state_known recom_vote recom_same recom_other recom_neutral recom_committee leak any_leak vote_committee deci_committee sdpoints male age

order sid mid gid gpid treatment tr_secrecy tr_transparency tr_leaks scenario scenround state red_state blue_state light_state dark_state red_s_sec red_s_leaks red_s_transp blue_s_sec blue_s_leaks blue_s_transp secr_sc1 secr_sc2 tran_sc1 tran_sc2 leak_sc1 leak_sc2 advisor color_type neutral_type dark_type light_type ball_color gray_ball light_ball dark_ball red_ball message comm_state eq_communication  state_known bad_state_known good_state_known recom_vote recom_same recom_other recom_neutral recom_committee leak any_leak vote_committee deci_committee sdpoints male age

cd $path
save alldata-rec.dta, replace


erase session_leaks_1.dta
erase session_leaks_2.dta
erase session_leaks_3.dta
erase session_secrecy_1.dta
erase session_secrecy_2.dta
erase session_secrecy_3.dta
erase session_transparency_1.dta
erase session_transparency_2.dta
erase session_transparency_3.dta

************************************************************************************************************************************************************
exit
